package com.csizg.facemodel.strategy;

import android.content.Context;
import com.csizg.facemodel.util.CompareResult;
import com.csizg.facemodel.util.FaceUtil;
import java.util.ArrayList;
import java.util.List;
import org.bytedeco.javacpp.opencv_face;

/**
 * LBPH人脸识别算策略
 *
 * @author haohao(ronghao3508@gmail.com) on 2018/5/23 20:12
 * @version v1.0
 */
public class LbphUpdateStrategy implements ICompareStrategy {

    private double mThreshold = 50d;//图像对比的阈值
    private List<String> hasTrainFiles = new ArrayList<>();
    private opencv_face.LBPHFaceRecognizer recognizer;

    public LbphUpdateStrategy() {
    }

    public LbphUpdateStrategy(double threshold) {
        this.mThreshold = threshold;
    }

    @Override
    public void setThreshold(double threshold) {
        this.mThreshold = threshold;
        if (recognizer == null) return;
        recognizer.setThreshold(mThreshold);
    }

    @Override
    public void init(Context context) {
        if (recognizer != null && !recognizer.getHistograms().isNull()) {
            return;
        }
        recognizer = opencv_face.createLBPHFaceRecognizer();
        FaceUtil.train(context, recognizer, hasTrainFiles);
        recognizer.setThreshold(mThreshold);
    }

    @Override
    public void prepare(Context context) {
        FaceUtil.update(context, recognizer, hasTrainFiles);
    }

    @Override
    public CompareResult compare(Context context, String face2) {
        return FaceUtil.predict(context, recognizer, face2);
    }
}
